package cn.fz.singleton;

import java.io.InputStream;
import java.util.Properties;

/**
 * 以一个简单的配置管理类为例，通过单例模式来管理应用程序中的配置项
 */
public class ConfigurationManager implements ISingletonMethod{
    private Properties properties;

    // 单例实例
    private static ConfigurationManager instance;

    // 私有构造方法
    private ConfigurationManager() {
        // 读取配置文件，初始化配置项
        properties = new Properties();
        try {
            InputStream stream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
            properties.load(stream);
            // properties.load(new BufferedReader(new FileReader("config.properties")));
            // properties.load(ConfigurationManager.class.getResourceAsStream("config.properties"));
            // properties.load(new FileInputStream("config.properties"));
        } catch (Exception e) {
            System.out.println("加载配置文件出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

    // 获取单例实例的全局访问方法
    public static synchronized ConfigurationManager getInstance() {
        if (instance == null) {
            instance = new ConfigurationManager();
        }
        return instance;
    }

    // 获取配置项的值
    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    @Override
    public void show() {
        System.out.println("简单的配置管理类: ConfigurationManager");
    }
}
